/*
 * @lc app=leetcode.cn id=72 lang=cpp
 *
 * [72] 编辑距离
 */

// @lc code=start
class Solution {
public:
    int minDistance(string word1, string word2) {
        int m = word1.length();
        int n = word2.length();
        if (m * n == 0) return m + n;
        vector<vector<int>> dp(m + 1, vector<int>(n + 1));

        for (int i = 0; i < m + 1; i++) {
            dp[i][0] = i;
        }
        for (int j = 0; j < n + 1; j++) {
            dp[0][j] = j;
        }

        for (int i = 1; i < m + 1; i++) {
            for (int j = 1; j < n + 1; j++) {
                int a = dp[i][j - 1] + 1;
                int b = dp[i - 1][j] + 1;
                int c = dp[i - 1][j - 1];
                if (word1[i - 1] != word2[j - 1]) c += 1;
                    
                dp[i][j] = min(a, min(b, c));
            }
        }
        return dp[m][n];
    }
};
// @lc code=end

